import { createRouter, createWebHashHistory,createWebHistory, RouteRecordRaw } from 'vue-router'
import { staticRoutes } from './constantRoutes'


const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

import loginStore from '@/store/modules/login/loginStore' 

router.beforeEach((to , from , next)=>{
  let user = loginStore();
  let token = user.token;
  if(token){
    if(to.path == "/login"){
      next("/index");
    }else{
      next();
    }
  }else{
    if(to.path == "/login"){
      next();
    }else{
      next("/login");
    }
  }
}
)





export default router
